$(function () {
	$("#jqGrid").jqGrid({
		url: '../informalCheck/list',
		datatype: "json",
		colModel: [			
			{ label: '员工工号', name: 'workNo', width: 80},
			{ label: '员工姓名', name: 'name', width: 60 }, 
			{ label: '岗位编号', name: 'jobNo', width: 80 }, 
			{ label: '总实收(元)', name: 'sumMoney', width: 80, formatter: function(value, options, row){
				if(value==0){
					return "<span style='color:red'>"+0+"</span>";
				}else if(value!=null){
					if(Number(value)>=20000){
						return "<span style='color:#16da16'>"+formatCurrency(value)+"</span>";
					}else if(Number(value)<12000){
						return "<span style='color:red'>"+formatCurrency(value)+"</span>";
					} else {
						return formatCurrency(value);
					}
				}else{
					return "";
				}				
				
			} }, 			
			{ label: '总应收(元)', name: 'shouldMoney',formatter: function(value, options, row){
				if(value==0){
					return 0;
				}else if(value!=null){
					return formatCurrency(value)
				}else{
					return "";
				}									
			}, width: 80 }, 	
			{ label: '近两个月开单实收(元)', name: 'twoMonthMoney', width: 90 , formatter: function(value, options, row){

				if(Number(value)==0){
					return "<span style='color:red'>"+0+"</span>";
				}else{
					return "<span style='color:#16da16'>"+formatCurrency(value)+"</span>";
				} 
			}  }, 	
			{ label: '总合同数(单)', name: 'sumContract', width: 80, formatter: function(value, options, row){

				if(Number(value)>1){
					return "<span style='color:#16da16'>"+formatCurrency(value)+"</span>";
				}else if(Number(value)<1){
					return "<span style='color:red'>"+0+"</span>";
				} else {
					return value;
				}
			}  }, 			
			{ label: '入职时间', name: 'inOrgTime', width: 80, formatter: function(value, options, row){
				return value.substring(0,10)} }	,	
			{ label: '工作时间(月)', name: 'workTime', width: 80 , formatter: function(value, options, row){

				if(Number(value)<3){
					return "<span style='color:red'>"+value+"</span>";
					
				}else{
					return "<span style='color:#16da16'>"+value+"</span>";
				}
			}},
			],
			viewrecords: true,
			 height: $(window).height()-155,
			rowNum: 10,
			rowList : [10,30,50],
			rownumbers: true, 
			rownumWidth: 25, 
			autowidth:true,
			multiselect: false,
			pager: "#jqGridPager",
			jsonReader : {
				root: "page.list",
				page: "page.currPage",
				total: "page.totalPage",
				records: "page.totalCount"
			},
			prmNames : {
				page:"page", 
				rows:"limit", 
				order: "order"
			},
			ondblClickRow:function(row){  
	            var rowData = $('#jqGrid').jqGrid('getRowData',row);  
	            look(rowData.workNo);  
	        }, 
			gridComplete:function(){
				$.ajax({
					url:'../datapayroll/date',
					success:function(data){
						vm.month=data.payrollmonth
					}
				})
				//隐藏grid底部滚动条
				$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 

			}
	});

});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		value:null,
		title: null,
		month:"",
		informalCheck: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.dataContractpayDetail = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
			vm.title = "修改";

			vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.informalCheck.id == null ? "../informalCheck/save" : "../informalCheck/update";
			$.ajax({
				type: "POST",
				url: url,
				data: JSON.stringify(vm.informalCheck),
				success: function(r){
					if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}

			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
					url: "../informalCheck/delete",
					data: JSON.stringify(ids),
					success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get("../informalCheck/info/"+id, function(r){
				vm.informalCheck = r.informalCheck;
			});
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
				postData:{'value': vm.value},
				page:page
			}).trigger("reloadGrid");
		}
	}
});
function look(workNo) {
	top.layer.open({
		type : 2,
		title : '新人卓越奖历史查询',
		area : [ '80%', '90%' ],
		//scrollbar: true,
		shadeClose : false,
		content : 'zjdc/informalcheckdetail.html?workNo=' +workNo,
		btn : [ '确认', '取消' ],
		yes : function(index, layero) {
			top.layer.close(index);
		}
	});
}
function formatCurrency(num) 
{
	if(num)
	{
		//将num中的$,去掉，将num变成一个纯粹的数据格式字符串
		num = num.toString().replace(/\$|\,/g,'');
		//如果num不是数字，则将num置0，并返回
		if(''==num || isNaN(num)){return 'Not a Number ! ';}
		//如果num是负数，则获取她的符号
		var sign = num.indexOf("-")> 0 ? '-' : '';
		//如果存在小数点，则获取数字的小数部分
		var cents = num.indexOf(".")> 0 ? num.substr(num.indexOf(".")) : '';
		cents = cents.length>1 ? cents : '' ;//注意：这里如果是使用change方法不断的调用，小数是输入不了的
		//获取数字的整数数部分
		num = num.indexOf(".")>0 ? num.substring(0,(num.indexOf("."))) : num ;
		//如果没有小数点，整数部分不能以0开头
		if('' == cents){ if(num.length>1 && '0' == num.substr(0,1)){return 'Not a Number ! ';}}
		//如果有小数点，且整数的部分的长度大于1，则整数部分不能以0开头
		else{if(num.length>1 && '0' == num.substr(0,1)){return 'Not a Number ! ';}}
		//针对整数部分进行格式化处理，这是此方法的核心，也是稍难理解的一个地方，逆向的来思考或者采用简单的事例来实现就容易多了
		/*
               也可以这样想象，现在有一串数字字符串在你面前，如果让你给他家千分位的逗号的话，你是怎么来思考和操作的?
               字符串长度为0/1/2/3时都不用添加
               字符串长度大于3的时候，从右往左数，有三位字符就加一个逗号，然后继续往前数，直到不到往前数少于三位字符为止
		 */
		for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		{
			num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
		}
		//将数据（符号、整数部分、小数部分）整体组合返回
		return (sign + num + cents);    
	}

}